OptimalStringAlignment

class OptimalStringAlignment(val insertionWeight: Double = Constants.DEFAULT_WEIGHT, val deletionWeight: Double = Constants.DEFAULT_WEIGHT, val substitutionWeight: Double = Constants.DEFAULT_WEIGHT, val transpositionWeight: Double = Constants.DEFAULT_WEIGHT) : StringSimilarity, StringDistance(source)

Implements the Optimal String Alignment algorithm, sometimes called the restricted edit distance variant of the Damerau-Levenshtein distance (Damerau, 1964).

The difference between the two algorithms consists in that the Optimal String Alignment algorithm computes the number of edit operations needed to make the strings equal under the condition that no substring is edited more than once, whereas Damerau-Levenshtein presents no such restriction.

Computes the distance between strings: the minimum number of operations needed to transform one string into the other (insertion, deletion, substitution of a single character, or a transposition of two adjacent characters) while no substring is edited more than once.

The similarity is computed as \(\frac{w_d \lvert X \rvert + w_i \lvert Y \rvert - distance(X, Y)}{2}\).

Note: although the DamerauLevenshtein distance is a metric distance, this is not true for the Optimal String Alignment algorithm. This is because it violates the triangle inequality: \(distance("CA", "AC") + distance("AC", "ABC") < distance("CA", "ABC")\), so it is not a true metric.

References

Damerau, F. J. (1964-03). A technique for computer detection and correction of spelling errors. Communications of the ACM, 7(3), 171-176. https://doi.org/10.1145/363958.363994[sci-hub]

Author

solonovamax

Parameters

insertionWeight

The weight of an insertion. Represented as \(w_i\). Must be in the range \([0, 1 \times 10^{10} ]\).

deletionWeight

The weight of a deletion. Represented as \(w_d\). Must be in the range \([0, 1 \times 10^{10} ]\).

substitutionWeight

The weight of a substitution. Represented as \(w_s\). Must be in the range \([0, 1 \times 10^{10} ]\).

transpositionWeight

The weight of a substitution. Represented as \(w_t\). Must be in the range \([0, 1 \times 10^{10} ]\).

See also

Constructors

Link copied to clipboard
constructor(insertionWeight: Double = Constants.DEFAULT_WEIGHT, deletionWeight: Double = Constants.DEFAULT_WEIGHT, substitutionWeight: Double = Constants.DEFAULT_WEIGHT, transpositionWeight: Double = Constants.DEFAULT_WEIGHT)

Properties

Link copied to clipboard

The weight of a deletion. Represented as \(w_d\).

Link copied to clipboard

The weight of an insertion. Represented as \(w_i\).

Link copied to clipboard

The weight of a substitution. Represented as \(w_s\).

Link copied to clipboard

The weight of a transposition. Represented as \(w_t\).

Functions

Link copied to clipboard
open override fun distance(s1: String, s2: String): Double

Computes the distance of two strings.

Link copied to clipboard
open override fun similarity(s1: String, s2: String): Double

Computes the similarity of two strings.